package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"red/api/request"
	"red/global"
	"red/intermal/model"
)

func List(c *gin.Context) {
	param := request.VideoList{}
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	page := param.Page
	if page <= 0 {
		page = 1
	}

	pageSize := param.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	var videolist []model.Video
	global.DB.Model(&model.Video{}).Offset(offset).Limit(pageSize).Where("type like ?", "%"+param.Type+"%").Find(&videolist)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "展示成功",
		"data": videolist,
	})
}

func Show(c *gin.Context) {
	param := request.VideoShow{}
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	var videos model.Video
	global.DB.Model(&model.Video{}).Where("id=?", param.Id).Find(&videos)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "详情成功",
		"data": videos,
	})
}

func Comment(c *gin.Context) {
	param := request.CommentAdd{}
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	comment := model.Comment{
		VideoId: int32(param.VideoId),
		UserId:  int32(param.UserId),
		Comment: param.Comment,
	}
	global.DB.Model(&model.Comment{}).Create(&comment)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "添加成功",
		"data": comment.Id,
	})
}
